package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysFactorInfo;
import com.emep.changzhi.analyse.model.Result;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysFactorInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 因子信息控制器
 * @date 2018/2/26 14:21
 */
@RestController
@RequestMapping("factor")
@Api(value="因子信息controller",tags={"因子信息"})
public class SysFactorInfoController {

    @Autowired
    private SysFactorInfoService sysFactorInfoService;

    /**
     * 查询所有因子信息
     * @return
     */
    @ApiOperation(value="获取因子信息列表", notes="获取因子信息列表")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<List<SysFactorInfo>> queryUser(){
        return new ResultBean<List<SysFactorInfo>>(sysFactorInfoService.findAll());
    }

    /**
     * 新增因子信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增因子信息", notes="新增因子信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "factorShrink", value = "因子简称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "factorName", value = "因子名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "factorActualName", value = "因子实际名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "factorType", value = "因子类型", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "factorStatus", value = "因子状态", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "factorSi", value = "因子单位", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "emissionStandardFineId", value = "细则id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "realShrink", value = "实时简称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "minuteShrink", value = "分钟简称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "hourShrink", value = "小时简称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "dayShrink", value = "日简称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "isPollutant", value = "是否是污染物", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "add")
    public ResultBean<SysFactorInfo> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "factorShrink","factorName","factorActualName"
                ,"factorType","factorStatus","factorSi","emissionStandardFineId","realShrink","minuteShrink","hourShrink","dayShrink","isPollutant");
        if (result.getCode() == 1){
            return result.boolAdd( sysFactorInfoService.save(payload));
        }
        return  result;
    }

    /**
     * 通过id 查询因子信息
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id查询因子信息", notes="根据id来获取因子信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "因子id", required = true, dataType = "String", paramType = "query")
    @GetMapping(value = "info")
    public ResultBean<SysFactorInfo> queryUser( @RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(sysFactorInfoService.findId(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 根据id 删除因子信息
     * @param payload
     */
    @ApiOperation(value="删除因子信息", notes="根据因子id 删除因子信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "因子id", required = true, dataType = "String",paramType = "query")
    @DeleteMapping(value = "/delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.bool(sysFactorInfoService.delete(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 分页查询因子信息列表
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询因子信息", notes="分页查询因子信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean<Page<SysFactorInfo>> showUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<SysFactorInfo> pageSysFactorInfo = sysFactorInfoService.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return result.ok(pageSysFactorInfo);
        }
        return result;
    }
    
}
